package com.rrd.mex.dw.mr.babel.parser;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSONArray;
import com.ucredit.babel.common.model.BaseModel;
import com.ucredit.babel.common.model.RecordType;
import com.ucredit.babel.common.model.tanzhi.BlacklistReportRecord;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import java.io.IOException;

/**
 * @Classname BlacklistReportRecordParse
 * @Description 探知-黑名单
 * @Date 2023/8/9 17:52
 * @Created by zxb
 */

public class BlacklistReportRecordParse extends ParseBase {
    Text key = new Text();
    Text value = new Text();

    @Override
    public boolean parseJob(Mapper.Context context, BaseModel<?> model, String successful) throws IOException, InterruptedException {
        BlacklistReportRecord obj = (BlacklistReportRecord) model;
        StringBuilder sb = getBaseInfo(obj, successful,1);

        sb.append(obj.getCode() == null ? "" : obj.getCode()).append(CRT001);
        sb.append(obj.getMsg() == null ? "" : obj.getMsg()).append(CRT001);
        sb.append(obj.getToken() == null ? "" : obj.getToken()).append(CRT001);

        JSONObject data = obj.getData() == null ? new JSONObject() : obj.getData();
        sb.append(formatValue(data.getString("hit"))).append(CRT001);

        JSONArray reasons = data.getJSONArray("reasons") == null ? new JSONArray() : data.getJSONArray("reasons");
        JSONObject reason = (JSONObject) reasons.get(0);
        sb.append(formatValue(reason.getString("hitPhoneNumber"))).append(CRT001);
        sb.append(formatValue(reason.getString("hitName"))).append(CRT001);
        sb.append(formatValue(reason.getString("hitBankNumber"))).append(CRT001);
        sb.append(formatValue(reason.getString("hitIdNumber"))).append(CRT001);

        key.set(getKey());
        value.set(sb.toString().replace("\n", " ").replace("\r", " "));
        context.write(key, value);
        return true;
    }

    @Override
    public String getTableName() {
        return "kafka_babel_tanzhi_blacklist_report_inc";
    }

    @Override
    public String getType() {
        return RecordType.BLACKLIST_REPORT_RECORD.toString();
    }

    @Override
    public String getComment() {
        return "探知-黑名单";
    }

    @Override
    public BaseModel getModel() {
        return new BlacklistReportRecord();
    }
}
